const mongoose = require("mongoose");
var User = mongoose.model("user");

exports.findAllUser = function(req, res) {
    User.find(function(err, users) {
        res.json(users);
    });
}
exports.addUser = function(req, res) {
    var u = new User(req.body);
    u.save(function(err) {
        if (!err) {
            res.json(u);
        } else {
            res.json({ error: "addUser" });
        }
    })
}
exports.loadUser = function(req, res) {
    User.findOne({ _id: req.params.id }, function(err, user) {
        if (user!=null) {
            res.json(user);
        } else { res.json({}) }
    })
}
exports.updateUser = function(req, res) {
    User.findByIdAndUpdate(req.params.id, req.body, function(err, oldUser) {
        //数据库的老数据
        req.body.id = req.params.id; //或者findone 。。。
        res.json(req.body);

    });
}
exports.deleteUser = function(req, res) {
    User.findOne({ _id: req.params.id }, function(err, user) {
        user.remove(function(err) {
            if (!err) {
                res.json({});
            } else {
                res.json({ error: "deleteUser" });
            }
        });
    });
}
